package ar.fiuba.tp7552.buildadmin.webactions.expensas;

import ar.fiuba.tp7552.buildadmin.model.BusinessException;
import ar.fiuba.tp7552.buildadmin.model.expensas.Cobro;
import ar.fiuba.tp7552.buildadmin.service.ExpensaService;
import ar.fiuba.tp7552.buildadmin.webactions.BaseAction;

import com.opensymphony.xwork2.Preparable;

public class CobroAction extends BaseAction implements Preparable {

	private ExpensaService expensaService;
	private Cobro cobro;
	
	// ~~ Constructor ====================================================
	
	public CobroAction(ExpensaService expensaService) {
		super();
		this.expensaService = expensaService;
	}
	
	// ~~ Actions ========================================================

	@Override
	public void prepare() throws Exception {
		if (cobro != null && cobro.getId() != null) {
			cobro = expensaService.getCobro(cobro.getId());
		}
	}
	
	public String load() {
		return BaseAction.SUCCESS;
	}
	
	public String consolidar() {
		try {
			expensaService.consolidar(cobro.getPropiedad(), cobro, cobro.getTipo(), cobro.getComprobante());
			return BaseAction.SUCCESS;
		} catch (BusinessException e) {
			addActionError(e.getMessage());
			return BaseAction.INPUT;
		}
	}
	
	public String delete() {
		expensaService.delete(cobro);
		return BaseAction.SUCCESS;
	}
	
	// ~~ getters y setters ==============================================

	public Cobro getCobro() {
		return cobro;
	}

	public void setCobro(Cobro cobro) {
		this.cobro = cobro;
	}

}
